! *************************************************************
 subroutine calfun(nparam_dum,param_dum,val)
 use mostly
 use tauchen_mod
 implicit none
 !integer, parameter:: prec=kind(1.d0)
 integer,intent(in)                 :: nparam_dum
 real(prec),dimension(:),intent(in) :: param_dum(nparam_dum)
 real(prec),intent(out)             :: val

 !integer :: mv
 real(prec),dimension(:), allocatable :: v_err

 !mv=10
 allocate(v_err(mv))

!01feb21
! call readinfo

if(calibrcase.eq.1) then
     phi1=(phi1_h-phi1_l)*param_dum(1)+phi1_l
     gamma_psi=(gamma_h-gamma_l)*param_dum(2)+gamma_l
     Psimin=(Psimin_h-Psimin_l)*param_dum(3)+Psimin_l
     beta=(beta_h-beta_l)*param_dum(4)+beta_l !18jan15
     Rbar(1)=(Rbar_h-Rbar_l)*param_dum(5)+Rbar_l
     Rbar(1:nR)=Rbar(1)
     Rbar_aver=sum(Rbar(1:nR)*piR(1:nR))/nR

    !02feb21
    IF(ID==0) THEN
    open(299, file='adjusted_param.dat')
    write(299,*) phi1,  gamma_psi, Psimin, beta, Rbar(1)
    close(299)
    END IF
    call MPI_BARRIER(MPI_COMM_WORLD,IERROR) !mpi
    open(299, file='adjusted_param.dat',status='old')
    read(299,*) phi1,  gamma_psi, Psimin, beta, Rbar(1)
    close(299)
    Rbar(1:nR)=Rbar(1)
    Rbar_aver=sum(Rbar(1:nR)*piR(1:nR))/nR

        call initstuff
        call prices_iter
        call simul_paths

        v_err(1)=   (AtoY-AtoY_dat)/AtoY_dat
        v_err(2)=   (freqdef-freqdef_dat)/freqdef_dat
        v_err(3)=   (arrears-arrears_dat)/arrears_dat
        v_err(4)=   (spr_stdv-sprstdv_dat)/sprstdv_dat
        v_err(5)=   (debt_stdv-debtstdv_dat)/debtstdv_dat
        v_err(6)=   (d_stdv-dstdv_dat)/dstdv_dat
        v_err(7)=   (corrSpr-(corrSpr_dat))/(corrSpr_dat)
        v_err(8)=   (Spr-Spr_dat)/Spr_dat
        v_err(9)=   (sdC-sdC_dat)/sdC_dat
        v_err(10)=   (smalldef-smalldef_dat)/smalldef_dat

        v_err(2)=   2.0_prec*v_err(2)
        v_err(3)=   2.0_prec*v_err(3)

        v_err(8)=   zero*v_err(8)
        v_err(9)=   zero*v_err(9)
        v_err(10)=   zero*v_err(10)
end if

if(calibrcase.eq.2) then
     phi1=(phi1_h-phi1_l)*param_dum(1)+phi1_l
     gamma_psi=(gamma_h-gamma_l)*param_dum(2)+gamma_l
     Psimin=(Psimin_h-Psimin_l)*param_dum(3)+Psimin_l
     beta=(beta_h-beta_l)*param_dum(4)+beta_l !18jan15

    IF(ID==0) THEN
    open(299, file='adjusted_param.dat')
    write(299,*) phi1,  gamma_psi, Psimin, beta
    close(299)
    END IF
    call MPI_BARRIER(MPI_COMM_WORLD,IERROR) !mpi
    open(299, file='adjusted_param.dat',status='old')
    read(299,*) phi1,  gamma_psi, Psimin, beta
    close(299)

        call initstuff
        call prices_iter
        call simul_paths

        v_err(1)=   (AtoY-AtoY_dat)/AtoY_dat
        v_err(2)=   (freqdef-freqdef_dat)/freqdef_dat
        v_err(3)=   (arrears-arrears_dat)/arrears_dat
        v_err(4)=   (debt_stdv-debtstdv_dat)/debtstdv_dat
        v_err(5)=   (d_stdv-dstdv_dat)/dstdv_dat

!        v_err(2)=   2.0_prec*v_err(2)
!        v_err(3)=   2.0_prec*v_err(3)

end if

if(calibrcase.eq.3) then
     phi1=(phi1_h-phi1_l)*param_dum(1)+phi1_l
     gamma_psi=(gamma_h-gamma_l)*param_dum(2)+gamma_l
     Psimin=(Psimin_h-Psimin_l)*param_dum(3)+Psimin_l
     beta=(beta_h-beta_l)*param_dum(4)+beta_l !18jan15

    IF(ID==0) THEN
    open(299, file='adjusted_param.dat')
    write(299,*) phi1,  gamma_psi, Psimin, beta
    close(299)
    END IF
    call MPI_BARRIER(MPI_COMM_WORLD,IERROR) !mpi
    open(299, file='adjusted_param.dat',status='old')
    read(299,*) phi1,  gamma_psi, Psimin, beta
    close(299)

        call initstuff
        call prices_iter
        call simul_paths

        v_err(1)=   (AtoY-AtoY_dat)/AtoY_dat
        v_err(2)=   (freqdef-freqdef_dat)/freqdef_dat
        v_err(3)=   (arrears-arrears_dat)/arrears_dat
        v_err(4)=   (debt_stdv-debtstdv_dat)/debtstdv_dat
        v_err(5)=   (d_stdv-dstdv_dat)/dstdv_dat
        v_err(6)=   (spr_stdv-sprstdv_dat)/sprstdv_dat

!        v_err(2)=   2.0_prec*v_err(2)
!        v_err(3)=   2.0_prec*v_err(3)

end if

if((calibrcase.eq.4).or.(calibrcase.eq.5)) then
     phi1=(phi1_h-phi1_l)*param_dum(1)+phi1_l
     gamma_psi=(gamma_h-gamma_l)*param_dum(2)+gamma_l
     Psimin=(Psimin_h-Psimin_l)*param_dum(3)+Psimin_l
     beta=(beta_h-beta_l)*param_dum(4)+beta_l !18jan15
     Rbar(1)=(Rbar_h-Rbar_l)*param_dum(5)+Rbar_l
     Rbar(1:nR)=Rbar(1)
     Rbar_aver=sum(Rbar(1:nR)*piR(1:nR))/nR


    IF(ID==0) THEN
    open(299, file='adjusted_param.dat')
    write(299,*) phi1,  gamma_psi, Psimin, beta, Rbar(1)
    close(299)
    END IF
    call MPI_BARRIER(MPI_COMM_WORLD,IERROR) !mpi
    open(299, file='adjusted_param.dat',status='old')
    read(299,*) phi1,  gamma_psi, Psimin, beta, Rbar(1)
    close(299)
    Rbar(1:nR)=Rbar(1)
    Rbar_aver=sum(Rbar(1:nR)*piR(1:nR))/nR

        call initstuff
        call prices_iter
        call simul_paths

        v_err(1)=   (AtoY-AtoY_dat)/AtoY_dat
        v_err(2)=   (freqdef-freqdef_dat)/freqdef_dat
        v_err(3)=   (arrears-arrears_dat)/arrears_dat
        v_err(4)=   (debt_stdv-debtstdv_dat)/debtstdv_dat
        v_err(5)=   (d_stdv-dstdv_dat)/dstdv_dat
        v_err(6)=   (spr_stdv-sprstdv_dat)/sprstdv_dat

        v_err(1)=   2.0_prec*v_err(1)
        v_err(6)=   0.5_prec*v_err(6)
!        v_err(2)=   1.5_prec*v_err(2)
!        v_err(3)=   1.5_prec*v_err(3)

!        v_err(2)=   2.0_prec*v_err(2)
!        v_err(3)=   2.0_prec*v_err(3)

end if


if((calibrcase.eq.6).or.(calibrcase.eq.7)) then
     phi1=(phi1_h-phi1_l)*param_dum(1)+phi1_l
     gamma_psi=(gamma_h-gamma_l)*param_dum(2)+gamma_l
     Psimin=(Psimin_h-Psimin_l)*param_dum(3)+Psimin_l
     beta=(beta_h-beta_l)*param_dum(4)+beta_l !18jan15
     Rbar(1)=(Rbar_h-Rbar_l)*param_dum(5)+Rbar_l
     Rbar(1:nR)=Rbar(1)
     Rbar_aver=sum(Rbar(1:nR)*piR(1:nR))/nR
     sdeta=(sdeta_h-sdeta_l)*param_dum(6)+sdeta_l
     call tauchen(zero, sdeta, rhoz, 3.0_prec, nz, zj, piz)
     zj(1:nz)=exp(zj(1:nz))/two

    IF(ID==0) THEN
    open(299, file='adjusted_param.dat')
    write(299,*) phi1,  gamma_psi, Psimin, beta, Rbar(1), sdeta
    close(299)
    END IF
    call MPI_BARRIER(MPI_COMM_WORLD,IERROR) !mpi
    open(299, file='adjusted_param.dat',status='old')
    read(299,*) phi1,  gamma_psi, Psimin, beta, Rbar(1), sdeta
    close(299)
    Rbar(1:nR)=Rbar(1)
    Rbar_aver=sum(Rbar(1:nR)*piR(1:nR))/nR
    call tauchen(zero, sdeta, rhoz, 3.0_prec, nz, zj, piz)
    zj(1:nz)=exp(zj(1:nz))/two

        call initstuff
        call prices_iter
        call simul_paths

        v_err(1)=   (AtoY-AtoY_dat)/AtoY_dat
        v_err(2)=   (freqdef-freqdef_dat)/freqdef_dat
        v_err(3)=   (arrears-arrears_dat)/arrears_dat
        v_err(4)=   (debt_stdv-debtstdv_dat)/debtstdv_dat
        v_err(5)=   (d_stdv-dstdv_dat)/dstdv_dat
        v_err(6)=   (spr_stdv-sprstdv_dat)/sprstdv_dat
        v_err(7)=   (sqrt(logy_var)-logy_sd_dat)/logy_sd_dat

        v_err(1)=   3.0_prec*v_err(1)
        v_err(6)=   1.25_prec*v_err(6)
        v_err(7)=   1.0_prec*v_err(7)

!        v_err(2)=   2.0_prec*v_err(2)
!        v_err(3)=   2.0_prec*v_err(3)

end if


if((calibrcase.eq.8)) then
     phi1=(phi1_h-phi1_l)*param_dum(1)+phi1_l
     gamma_psi=(gamma_h-gamma_l)*param_dum(2)+gamma_l
     Psimin=(Psimin_h-Psimin_l)*param_dum(3)+Psimin_l
     beta=(beta_h-beta_l)*param_dum(4)+beta_l !18jan15
     Rbar(1)=(Rbar_h-Rbar_l)*param_dum(5)+Rbar_l
     Rbar(1:nR)=Rbar(1)
     Rbar_aver=sum(Rbar(1:nR)*piR(1:nR))/nR
     sdeta=(sdeta_h-sdeta_l)*param_dum(6)+sdeta_l
     call tauchen(zero, sdeta, rhoz, 3.0_prec, nz, zj, piz)
     zj(1:nz)=exp(zj(1:nz))/two

    IF(ID==0) THEN
    open(299, file='adjusted_param.dat')
    write(299,*) phi1,  gamma_psi, Psimin, beta, Rbar(1), sdeta
    close(299)
    END IF
    call MPI_BARRIER(MPI_COMM_WORLD,IERROR) !mpi
    open(299, file='adjusted_param.dat',status='old')
    read(299,*) phi1,  gamma_psi, Psimin, beta, Rbar(1), sdeta
    close(299)
    Rbar(1:nR)=Rbar(1)
    Rbar_aver=sum(Rbar(1:nR)*piR(1:nR))/nR
    call tauchen(zero, sdeta, rhoz, 3.0_prec, nz, zj, piz)
    zj(1:nz)=exp(zj(1:nz))/two

        call initstuff
        call prices_iter
        call simul_paths

        v_err(1)=   (AtoY-AtoY_dat)/AtoY_dat
        v_err(2)=   (freqdef-freqdef_dat)/freqdef_dat
        v_err(3)=   (arrears-arrears_dat)/arrears_dat
        v_err(4)=   (debt_stdv-debtstdv_dat)/debtstdv_dat
        v_err(5)=   (d_stdv-dstdv_dat)/dstdv_dat
        v_err(6)=   (spr_stdv-sprstdv_dat)/sprstdv_dat
        v_err(7)=   (sqrt(logy_var)-logy_sd_dat)/logy_sd_dat
        v_err(8)=   (debtserv-debtserv_dat)/debtserv_dat
        v_err(9)=   (debtserv_sd-debtserv_sd_dat)/debtserv_sd_dat

        v_err(1)=   3.0_prec*v_err(1)
        v_err(6)=   1.25_prec*v_err(6)
        v_err(7)=   1.0_prec*v_err(7)

!        v_err(2)=   2.0_prec*v_err(2)
!        v_err(3)=   2.0_prec*v_err(3)

end if

!!!!!!!!!!off
    call MPI_BARRIER(MPI_COMM_WORLD,IERROR) !mpi

    val=sqrt(sum(v_err(:)**2._prec))

IF(ID==0) THEN
       write(44,'(i6,11f9.4,3f6.3,f7.3,f7.3,f7.3,f7.3,f7.3,f7.3)') step_calib, Rbar_aver,  &
        & phi1, gamma_psi, Psimin, beta, sdeta,   &
        & sqrt(sum(v_err(:)**2._prec)), &
        & AtoY, Spr, freqdef, arrears, &
        & d_stdv, spr_stdv, &
        & debt_stdv, &
        & corrSpr, sdC, smalldef, sqrt(logy_var), debtserv, debtserv_sd
END IF

    step_calib=step_calib+1

 end subroutine calfun
